# -*- coding: UTF-8 -*-
# Author: Damon(wuud1)
# CreateDate: 
# Message:
from MysqlTools import MysqlTools
import json
import os
from PIL import Image

class clean_imgPoint:
    def __init__(self, **kwargs):
        self.db = MysqlTools(db_config=kwargs.get("db_config"), type="dict", auto_commit=1000)
        self.struc_table = kwargs.get("struc_table")
        self.parts_table = kwargs.get("parts_table")
        self.pth = '/Users/duanmengdi/Desktop/workspace/py3_code/hk/hyundai_img/CCHNPSQ18_format/'

    def get_wh(self, mid):
        img_pth = self.pth+mid+'.png'
        pic = Image.open(img_pth)
        return pic.size

    def get_imgPoint_info(self, mid):
        # 获取图片宽高
        x = 0.68
        width, height = self.get_wh(mid)
        sql = '''select * from `{}` where `mid`="{}"'''.format(self.parts_table, mid)
        data = self.db.select_all(sql)
        _dict = {"hotspots": [], "imageHeight": height, "imageWidth": width, "imageFormat": "image/png", "maxScaleFactor": 1.0}
        for info in data:
            hskey = info.get("itid")
            imgpoint = json.loads(info.get("img"))
            hsX = int(int(imgpoint[0])*0.68)
            hsY = int(int(imgpoint[1])*0.68)
            maxX = int((int(imgpoint[0])+int(imgpoint[2]))*0.68)
            maxY = int((int(imgpoint[1])+int(imgpoint[3]))*0.68)
            _d = {"hsX": hsX, "hsY": hsY, "maxX": maxX, "maxY": maxY, "hsKey": hskey}
            _dict["hotspots"].append(_d)
        return _dict

    def run(self):
        # 清洗描点
        sql = '''select * from `{}`'''.format(self.struc_table)
        data = self.db.select_all(sql)
        for info in data:
            id = info.get("id")
            mid = info.get("mid")
            imgPoint_info = self.get_imgPoint_info(mid)
            up_sql = '''update `{}` set `imgPoints`='{}' where `id`="{}";'''.format(self.struc_table, json.dumps(imgPoint_info, ensure_ascii=False), id)
            self.db.execute_sql(up_sql)
        self.db.commit_sql()

    def clean_part(self):
        sql = '''select `id`, `itid`, `extra_info` from `{}`'''.format(self.parts_table)
        data = self.db.select_all(sql)
        for info in data:
            id = info.get("id")
            itid = info.get("itid")
            extra_info = json.loads(info.get("extra_info"))
            extra_info["itid"] = itid
            extra_info = json.dumps(extra_info, ensure_ascii=False)
            up_sql = '''update `{}` set `extra_info`='{}' where `id`='{}';'''.format(self.parts_table, extra_info, id)
            self.db.execute_sql(up_sql)
        self.db.commit_sql()




if __name__ == '__main__':
    db_config = dict(
        host="127.0.0.1",
        port=3306,
        user="root",
        passwd="123456",
        db="kia",
        charset="utf8",
    )
    struc_table = 'cars_structure_collection_add'
    parts_table = 'cars_part_num_add'
    tools = clean_imgPoint(
        db_config=db_config,
        struc_table=struc_table,
        parts_table=parts_table
    )
    # tools.clean_part()
    tools.run()